package com.ly.mpayment.persist.service.uprechargerecord;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.PaginationUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.UpRechargeRecord;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;

@Service
public class UpRechargeRecordServiceImpl extends AbstractService<UpRechargeRecord, Serializable> implements IUpRechargeRecordService{

	@Override
	public boolean addUprechargerecord(UpRechargeRecord upreach) {
		if(upreach != null){
			this.saveOrUpdate(upreach);
			return true;
		}
		return false;
	}

	@Override
	public boolean updateUprechargerecord(UpRechargeRecord upreach) {
		if(upreach != null){
			this.updateEntity(upreach);
			return true;
		}
		return false;
	}

	@Override
	public UpRechargeRecord findById(String id) {
		return this.findUnique("select u from UpRechargeRecord u where u.rechargestatus = 1 and u.uprechargeuuid = ?", id);
	}

	@Override
	public Page<UpRechargeRecord> findPageUprechargerecord(Map<String, Object> parameters, int start, int pageSize) {
		String hql = "select u from UpRechargeRecord u left join fetch u.user uu where u.rechargestatus = 1 and uu.usertype = 4 ";
		if(parameters.containsKey(":starttime")){
			hql+= "  and date_format(u.rechargetime,'%Y-%m-%d') >= '"+parameters.get(":starttime").toString().concat("' ");
			parameters.remove(":starttime");
		}
		if(parameters.containsKey(":endtime")){
			hql+= " and date_format(u.rechargetime,'%Y-%m-%d') <= '"+parameters.get(":endtime").toString().concat("' ");
			parameters.remove(":endtime");
		}
		if(parameters.containsKey(":useruuid")){
			hql+= " and uu.useruuid ='".concat(parameters.get(":useruuid").toString().concat("' "));
			parameters.remove(":useruuid");
		}
		
		
		hql += " order by u.rechargetime desc ";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}

	@Override
	public List findRecharge() {
		return this.find("select u from UpRechargeRecord u");// where u.platFormAccount.platformuuid is null
	}

	@Override
	public String findUpRecordCount(Map<String, Object> parameters, int start, int pageSize) {

		String hql = "select sum(d.rechargeamount),u.username from uprechargerecord d,user u  where d.rechargestatus = 1 and u.useruuid = d.useruuid  ";
		if(parameters.containsKey(":starttime")){
			hql+= "  and date_format(d.rechargetime,'%Y-%m-%d') >= '"+parameters.get(":starttime").toString().concat("' ");
			parameters.remove(":starttime");
		}
		if(parameters.containsKey(":endtime")){
			hql+= " and date_format(d.rechargetime,'%Y-%m-%d') <= '"+parameters.get(":endtime").toString().concat("' ");
			parameters.remove(":endtime");
		}
		if(parameters.containsKey(":useruuid")){
			hql+= " and d.useruuid = '"+parameters.get(":useruuid").toString().concat("' ");
			parameters.remove(":useruuid");
		}
		hql += " group by d.useruuid order by d.createtime ";
		Page page = new Page();
		page.setStart(start);
		page.setPageSize(pageSize);
		String sql = "select * from ("+hql+") tt";
		Page pages = PaginationUtil.queryPaginationSQL(sql, page, this.getSession());
		List lists = pages.getResult();
		JSONArray arrays = JsonUtils.getJSONARRAY();
		if(lists != null && !lists.isEmpty()){

			for(int i = 0; i < lists.size() ; i ++){
				Object [] objs = (Object[]) lists.get(i);
				JSONObject obj = JsonUtils.getJSONOBJCT();
				obj.put("rechargeamount", objs[0] == null ? "" : objs[0].toString());
				obj.put("username", objs[1] == null ? "" : objs[1].toString());
				arrays.add(obj);
			}
		}
		JSONObject result = JsonUtils.getJSONOBJCT();
		result.put("result", arrays);
		result.put("totalCount", pages.getTotalCount());
		return result.toString();
	}
}
